package com.manlost.backoffice.controller.tag;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.manlost.biz.ManlostServiceLocator;
import com.manlost.biz.baby.model.constants.BabyType;
import com.manlost.biz.tag.BackTagService;
import com.manlost.biz.tag.model.constants.TagInfoStatus;
import com.manlost.biz.tag.model.constants.TagType;
import com.manlost.biz.tag.model.dataobject.Tag;
import com.manlost.biz.tag.model.dataobject.TagGroup;
import com.manlost.biz.tag.model.dto.TagDTO;
import com.manlost.biz.tag.model.dto.TagGroupDTO;

/**
 * 类TagManageController.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-3-31 下午6:37:47
 */
@Controller
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TagManageController {

    BackTagService backTagService = ManlostServiceLocator.getBackTagService();

    @RequestMapping("/tag/tagManage.htm")
    public String index(Map model) {
        addModelInfo(model);
        return "tag/tagManage";
    }

    @RequestMapping(value = "/tag/tagManage.htm", params = "method=addTagGroup", method = RequestMethod.POST)
    public String addTagGroup(Map model, String tagGroupName) {
        TagGroup tagGroup = new TagGroup();
        tagGroup.setName(tagGroupName);
        tagGroup.setStatus(TagInfoStatus.ENABLE.getValue());
        backTagService.addTagGroup(tagGroup);

        addModelInfo(model);
        return "tag/tagManage";
    }

    @RequestMapping(value = "/tag/tagManage.htm", params = "method=addTag", method = RequestMethod.POST)
    public String addTag(Map model, String tagName, Long tagGroupId) {
        Tag tag = new Tag();
        tag.setName(tagName);
        tag.setTagGroupId(tagGroupId);
        tag.setStatus(TagInfoStatus.ENABLE.getValue());
        backTagService.addTag(tag);

        addModelInfo(model);
        return "tag/tagManage";
    }

    /**
     * @param model
     */
    private void addModelInfo(Map model) {
        List<TagGroupDTO> tagGroups = backTagService.listTagGroupDTO();
        List<TagDTO> tags = backTagService.listTagDTO();

        model.put("tagTypeMap", TagType.getTagTypeMap());
        model.put("babyTypeMap", BabyType.getBabyTypeMap());
        model.put("tagGroups", tagGroups);
        model.put("tags", tags);
    }
}
